#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <boost/format.hpp>

#include "ArgsParser.h"

using namespace std;

#if _MSC_VER
#	define strcmpi _strcmpi
#	define strnicmp _strnicmp
#elif __GNUC__
#	define strcmpi strcasecmp
#	define strnicmp strncasecmp
#endif


ArgsParser::ArgsParser(int argc, const char *argv[])
{
	// Default settings
	_port = 80;
	_showStat = false;
	_serverType = SingleDispatcher;
	_serverThreadCount = 1;

	// Parsing command line arguments
	int argn = 1;

	while (argn < argc)
	{
		if (strcmpi(argv[argn], "-tbb") == 0)
		{
			_serverType = SingleDispatcher;
			++argn;
		}
		if (strnicmp(argv[argn], "-mt:", 4) == 0)
		{
			_serverType = Multithread;
			_serverThreadCount = atoi(argv[argn] + 4);
			++argn;
		}
		else if (strcmpi(argv[argn], "-simple") == 0)
		{
			_serverType = Simple;
			++argn;
		}
		else if (strcmpi(argv[argn], "-stat") == 0)
		{
			_showStat = true;
			++argn;
		}
		else if (strnicmp(argv[argn], "-port:", 6) == 0)
		{
			_port = atoi(argv[argn] + 6);
			++argn;
		}
		else
		{
			throw invalid_argument((boost::format("%1% is not a supported command line argument") % argv[argn]).str().c_str());
		}
	}
}